﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;


namespace EAGL.Data
{
    /// <summary>
    /// This class is used to fill feature class with records using the supplied cursor.
    /// </summary>
    public class FeatureClassAdapter
    {

        #region ========================= PROTECTED ========================

        private IFeatureCursor cursor;
        private Hashtable fieldMapping;
         
        /// <summary>
        ///  Maps the fields of the featuer class to field indexes in feature cursor
        /// </summary>
        /// <param name="table"></param>
        private void mapFields(IFeatureClass featureClass)
        {
            fieldMapping = new Hashtable();
            int c = featureClass.Fields.FieldCount;
            int i = 0;
            string s = null;
            int x = 0;
            while (i < c)
            {
                s = featureClass.Fields.get_Field(i).Name;
                x = cursor.Fields.FindField(s);
                fieldMapping.Add(s, x);
                i++;
            }
        }
        
        /// <summary>
        /// Fills the feature class with records
        /// </summary>
        /// <param name="table">feature class to be filled</param>
        /// <returns>the number of inserted records</returns>
        private int fillByCursor(IFeatureClass featureClass)
        {
            ITable table = featureClass as ITable;
            ICursor insertCursor = table.Insert(false);
            IFeature feature;
            int count = 0;
            int c = table.Fields.FieldCount;
            int i, x;
            mapFields(featureClass);
            
            while ((feature = cursor.NextFeature()) != null)
            {
                i = 0;
                IRowBuffer rb = table.CreateRow() as IRowBuffer;
                while (i < c)
                {
                    string name = table.Fields.get_Field(i).Name;
                    if (name == "FID")
                    {
                        i++;
                        continue;
                    }
                    x = (int)fieldMapping[name];
                    System.Object o = feature.get_Value(x);
                    rb.set_Value(i, o);
                    i++;
                }
                insertCursor.InsertRow(rb);
                count++;
            }
            cursor.Flush();
            return count;
        }


        #endregion


        #region ========================= PUBLIC ============================
        
        public FeatureClassAdapter(IFeatureCursor cursor)
        {
            this.cursor = cursor;
        }

        
        /// <summary>
        /// Fills a feature class with data supplied in the cursor
        /// </summary>
        /// <param name="featuerClass">feature class to be filled with data</param>
        /// <returns>the number of inserted rows</returns>
        public int Fill(IFeatureClass featuerClass)
        {
            try
            {
                int c = this.fillByCursor(featuerClass);
                return c;
            }
            catch (Exception ex)
            {
                throw new Exception("Error in fill function, " + ex.Message); 
            }
        }
        #endregion

        
    }
}
